package calorie.com.calorie.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.Md5Utils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.contact.ModifyLoginPwdContact;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;


/**
 * 修改登录密码
 */
public class ModifyLoginPwdActivity  extends MvpActivity<ModifyLoginPwdContact.ModifyLoginPwdPresent> implements ModifyLoginPwdContact.IModifyLoginPwd {
    @BindView(R.id.et_current_pwd)
    EditText etCurrentPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.et_confirm_new_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.btn_confirm_modify)
    Button btnConfirmModify;


    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_login_pwd;
    }


    @OnClick(R.id.btn_confirm_modify)
    public void onViewClicked() {
        String oldPwd = etCurrentPwd.getText().toString();
        String newPwd = etNewPwd.getText().toString();
        String confirmPwd = etConfirmPwd.getText().toString();
        if (ObjectUtils.isEmpty(oldPwd)) {
            ToastUtils.showLongToast(context, "旧密码不能为空");
            return;
        }
        if (ObjectUtils.isEmpty(newPwd)) {
            ToastUtils.showLongToast(context, "新密码不能为空");
            return;
        }
        if (ObjectUtils.isEmpty(confirmPwd)) {
            ToastUtils.showLongToast(context, "确认新密码不能为空");
            return;
        }
        if (!ObjectUtils.equals(newPwd, confirmPwd)) {
            ToastUtils.showLongToast(context, "两次新密码输入不一致");
            return;
        }
        if (!RegexUtils.isLoginPwd(newPwd)) {
            ToastUtils.showLongToast(context, getString(R.string.login_pwd_regex));
            return;
        }
       // showLoadingDialog();
        //网络请求
        int userId = StaticValue.user.getId();

        mPresent.modifyLoginPwd(userId, Md5Utils.stringToMD5(oldPwd), Md5Utils.stringToMD5(newPwd));

    }

    @Override
    public void getUpdatePassword(Response<ResultDTO> resultDTOResponse) {
        String code = resultDTOResponse.body().getCode();
        if (code.equals("200")) {
            ToastUtils.showLongToast(context, "修改成功");
            this.finish();

        } else if (code.equals("201")) {
            ToastUtils.showLongToast(context, "服务器异常");
        } else if (code.equals("202")) {
            ToastUtils.showLongToast(context, "旧密码错误");
        }
    }

    @Override
    protected ModifyLoginPwdContact.ModifyLoginPwdPresent createPresent() {
        return  new ModifyLoginPwdContact.ModifyLoginPwdPresent();
    }
}
